package org.jeecg.modules.flowable.service.action.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.jeecg.modules.flowable.common.util.validation.AssertValid;
import org.jeecg.modules.flowable.common.util.validation.MustParamValidation;
import org.jeecg.modules.flowable.dic.MatterFlowStatus;
import org.jeecg.modules.flowable.entity.TActMatterInstEntity;
import org.jeecg.modules.flowable.mapper.TActMatterInstMapper;
import org.jeecg.modules.flowable.mapper.TActTheendMapper;
import org.jeecg.modules.flowable.service.FlowProcService;
import org.jeecg.modules.flowable.service.FlowableService;
import org.jeecg.modules.flowable.service.action.ActionBody;
import org.jeecg.modules.flowable.service.action.ActionExec;
import org.jeecg.modules.flowable.system.core.SystemUser;
import org.jeecg.modules.flowable.system.etc.SystemUserUtil;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Title: 流程撤消 <br>
 * Desc:  <br>
 * Date: 6/26/22 10:03 AM <br>
 * Company: 联通（辽宁）产业互联网有限公司 <br>
 *
 * @author zxd
 * @version 1.0.0
 */
@Service("Undo")
@Slf4j
public class Undo implements ActionExec {
    @Autowired
    private FlowProcService flowProcService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private TActTheendMapper tActTheendMapper;

    @Autowired
    private TActMatterInstMapper tActMatterInstMapper;

    @Autowired
    private FlowableService flowableService;

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deal(ActionBody argActionBody,String argActionFunname,String argActionFuncode) {
        MustParamValidation.mustValidation(argActionBody.getMatterInstId(),argActionBody.getTaskId());
        SystemUser loginUser = SystemUserUtil.getSystemUser();
        DateTime now = DateTime.now();
        // 第一步：删除执行流
        List<Task> list = taskService.createTaskQuery().taskId(argActionBody.getTaskId()).list();
        runtimeService.deleteProcessInstance(list.get(0).getProcessInstanceId(),"执行撤消动作，实例被删除");
        // 第二步：重置事项处理状态
        LambdaQueryWrapper<TActMatterInstEntity> qq = new LambdaQueryWrapper<>();
        qq.eq(TActMatterInstEntity::getId,argActionBody.getMatterInstId());
        TActMatterInstEntity inst = tActMatterInstMapper.selectList(qq).get(0);
        TActMatterInstEntity tActMatterInstEntity = new TActMatterInstEntity();
        tActMatterInstEntity.setStatus(MatterFlowStatus.START);
        tActMatterInstEntity.setProcInstId("");
        if(AssertValid.isNotEmpty(argActionBody.getAttDesc())) {
            tActMatterInstEntity.setRemark("流程被撤销，撤消原因："+argActionBody.getAttDesc());
        } else {
            tActMatterInstEntity.setRemark("流程被撤销，未说明原因");
        }
        tActMatterInstMapper.update(tActMatterInstEntity,qq);
        return true;
    }
}